<?php
	if(!isset($rootdir)) $rootdir = "/home/nrb62281/public_html";
	include_once($rootdir . "/wowsig/DataObjects/characterData.php");

	class armoryInterfaceDataSource{
		public function getCharacter($characterName, $realmName, $region){
			$baseUrl = "http://www.wowarmory.com/character-sheet.xml?rhtml=n";
			
			if(($characterName != null)&&(strlen($characterName)>2)
				&&($realmName != null)&&(strlen($realmName)>2)
//				&&($region != null)&&(/*region validation*/1)
			){
				$url = $baseUrl . "&r=" . $realmName . "&n=" . $characterName;
				$xmlstring = file_get_contents($url);
				if($xmlstring == false) return null;
				$xml = simplexml_load_string($xmlstring);
				if($xml == false) return null;
			
				$returnValue = new characterData;
				
				// error checking
				$errorCheck = $xml->characterInfo[0]->attributes();
				if(isset($errorCheck["errCode"])) return null;
				
				$charAttributes = $xml->characterInfo[0]->character[0]->attributes();
				$pvpInfo = $xml->characterInfo[0]->characterTab[0]->pvp[0]->lifetimehonorablekills[0]->attributes();
				
				$specInfo = $xml->characterInfo[0]->characterTab[0]->talentSpecs[0];
				
				$returnValue->setNamePrefix($charAttributes["prefix"]);
				$returnValue->setNameCharacter($charAttributes["name"]);
				$returnValue->setNameSuffix($charAttributes["suffix"]);
				$returnValue->setGuildName($charAttributes["guildName"]);
				$returnValue->setLevel($charAttributes["level"]);
				$returnValue->setClass($charAttributes["class"]);
				$returnValue->setAchievementPoints($charAttributes["points"]);
				$returnValue->setFaction($charAttributes["faction"]);
				$returnValue->setLifetimePVPKills($pvpInfo["value"]);
				$returnValue->setRace($charAttributes["race"]);
				$returnValue->setGender(strtolower($charAttributes["gender"]));
				$returnValue->setRealmName($charAttributes["realm"]);
				
				$primarySpec = -1;
				
				if(sizeof($specInfo->talentSpec) > 1){
					$talentSpecOneAttributes = $specInfo->talentSpec[0]->attributes();
					if(isset($talentSpecOneAttributes["active"])){
						$primarySpec = 0;
					}else{
						$primarySpec = 1;
					}
					
				}else{
					$primarySpec = 0;
					
				}
				
				
				return $returnValue;
			}
		}
	}
?>